package com.fancy.learncenter.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.FragPagerAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.PersonCenterDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.fragment.FancyPicFragment;
import com.fancy.learncenter.fragment.ToothTZFragment;
import com.fancy.learncenter.fragment.XianZhiFragment;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.SPUtils;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

public class PersonInfoActivity extends BaseActivity {

    public static String USER_KEY = "userId";
    public static String PERSON_KEY = "PERSON_KEY";

    @Bind(R.id.tabs)
    TabLayout tabs;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    FragPagerAdapter mAdapter;
    List<Fragment> fragments = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    String userId;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.my_follow)
    TextView myFollow;
    @Bind(R.id.iamge_user)
    SimpleDraweeView iamgeUser;
    @Bind(R.id.follow_me)
    TextView followMe;
    @Bind(R.id.praise_bt)
    TextView praiseBt;
    @Bind(R.id.msg)
    ImageView msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info, Color.parseColor("#fda712"));
        ButterKnife.bind(this);


        userId = getIntent().getStringExtra(USER_KEY);


        if (userId.equals(SPUtils.getUserID())) {
            praiseBt.setVisibility(View.GONE);
            msg.setVisibility(View.GONE);

        }

        titles.add("帖子");
        titles.add("闲置");
        titles.add("视频");
        fragments.add(ToothTZFragment.newInstance(userId));
        fragments.add(XianZhiFragment.newInstance(userId));
        fragments.add(FancyPicFragment.newInstance(userId));
        mAdapter = new FragPagerAdapter(getSupportFragmentManager(), fragments, titles);
        //设置 NestedScrollView 的内容是否拉伸填充整个视图，
        //这个设置是必须的，否者我们在里面设置的ViewPager会不可见
        viewPager.setAdapter(mAdapter);
        tabs.setupWithViewPager(viewPager);

        getDatas();
    }

    String userName = "";

    private void getDatas() {
        Map map = new HashMap();
        map.put("userId", "" + userId);
        map.put("myId", "" + MyApplication.userId);
        map.put("token", "" + MyApplication.token);
        HttpMehtod.getInstance().baseDetail(map, new IdeaObserver<BaseDataBean<PersonCenterDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<PersonCenterDataBean> listBaseDataBean) {

                myFollow.setText(listBaseDataBean.getData().getFollow_num());

                followMe.setText(listBaseDataBean.getData().getFuns_num());

                iamgeUser.setImageURI(listBaseDataBean.getData().getImgsrc());

                userName = listBaseDataBean.getData().getNick_name();
                name.setText(listBaseDataBean.getData().getNick_name());

                if (listBaseDataBean.getData().getIs_follow() == 0) {

                    mfollow = false;
//                    praise_bt.setBackgroundResource(R.drawable.follow_bg);
                    praiseBt.setText("+关注");
                } else {
                    mfollow = true;
                    praiseBt.setText("-已关注");
//                    praise_bt.setBackgroundResource(R.drawable.follow_cancer_bg);

                }

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }


    //未关注
    boolean mfollow = false;

    private void doFollow() {
        Map map = new HashMap();
        map.put("tUserId", userId);

        HttpMehtod.getInstance().doFollow(map, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                mfollow = true;
                praiseBt.setText("已关注");
//                praiseBt.setBackgroundResource(R.drawable.follow_cancer_bg);

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

    private void cancelFollow() {
        Map map = new HashMap();
        map.put("tUserId", userId);

        HttpMehtod.getInstance().cancelFollow(map, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                mfollow = false;
                praiseBt.setText("关注");
//                praiseBt.setBackgroundResource(R.drawable.follow_bg);

            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }


    @OnClick({R.id.back, R.id.praise_bt, R.id.msg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.praise_bt:

                if (mfollow) {
                    cancelFollow();
                } else {
                    doFollow();
                }

                break;
            case R.id.back:
                finish();
                break;
            case R.id.msg:
                RongIM.getInstance().startPrivateChat(this, userId, userName);
                break;
        }
    }
}
